package com.example.controller;

import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/4/17
 * @time: 18:18
 */
@RestController
@RequestMapping("home")
@Api(tags = "home页信息请求类")
public class HomeController {
    /**
     * 获取系统信息
     * @return
     */
    @GetMapping("getSystemInfo")
    @ResponseBody
    public ResponseData getSystemInfo(){
        Map<String,String> systemInfo = new HashMap<>();
        try {
            Properties props = System.getProperties();
            systemInfo.put("osName", props.getProperty("os.name"));  //系统名称
            systemInfo.put("osArch", props.getProperty("os.arch"));  //系统框架
            systemInfo.put("osVersion", props.getProperty("os.version")); //操作系统版本
            systemInfo.put("javaVersion", props.getProperty("java.version")); //java运行时版本
            systemInfo.put("javaVendor", props.getProperty("java.vendor")); //java运行时版本
            systemInfo.put("javaRul", props.getProperty("java.vendor.url"));
            systemInfo.put("javaExtDirs", props.getProperty("java.ext.dirs"));

            InetAddress inetAddress = InetAddress.getLocalHost();
            systemInfo.put("localIp", inetAddress.getHostAddress());
            systemInfo.put("localHostName",inetAddress.getHostName());
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress);
            byte[] macAddress = networkInterface.getHardwareAddress();
            StringBuilder stringBuilder = new StringBuilder();
            for (int i=0;i<macAddress.length;i++){
                stringBuilder.append(String.format("%02X%s", macAddress[i], (i < macAddress.length - 1) ? "-" : ""));
            }
            systemInfo.put("localHostMac",stringBuilder.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), systemInfo);
    }
}
